class Cpanel::BannersController < Cpanel::BaseController

  def index
    @index_banners = Banner.index
    @post_banners = Banner.post
    @job_banners = Banner.job
    @product_banners = Banner.product
    @description_banners = Banner.description
    @service_banners = Banner.service
    @customer_banners = Banner.customer
    @partner_banners = Banner.partner
    @contact_banners = Banner.contact
  end

  def new
    @banner = Banner.new
  end

  def create

    @banner = Banner.new(banner_params)

    if params[:banner][:category] == 'index' && Banner.index.count > 6
      flash[:notice] = "banner count max is six"
      render 'new'

   elsif params[:banner][:category] == 'post' && Banner.post.count > 0
      flash[:notice] = "post banner is exist, please delete it"
      render 'new'
   else
      if @banner.save
        redirect_to cpanel_banners_path, notice: 'create success'
      else
        flash[:notice] = 'create field'
        render 'new'
      end
   end
  end

  def destroy
    @banner  = Banner.find params[:id]
    @banner.destroy

    redirect_to cpanel_banners_path, notice: 'delete success'
  end

  private

  def banner_params
    params.require(:banner).permit(:img, :category)
  end

end
